package cn.zhiwen.wxcontroller;

import cn.zhiwen.commonurl.UrlData;
import cn.zhiwen.entity.WxData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.security.MessageDigest;
import java.util.Arrays;

@RestController
public class WxDataController {

    @Value("${wx_token}")
    private String token;
    @Value("${wx_appid}")
    private String appid;
    @Value("${wx_grant_type}")
    private String grant_type;
    @Value("${wx_secret}")
    private String secret;
    @Value("${wx_EncodingAESKey}")
    private String EncodingAESKey;
    @Value("${wx_access_token}")
    private String access_token;

    @Autowired
    private RestTemplate restTemplate;


    //加密
    private MessageDigest messageDigest;

    /**
     * 获取access_token
     *
     * @return
     */
    @RequestMapping("getToken")
    public String getToken() {
        try {
            String forObject = restTemplate.getForObject(UrlData.access_token, String.class, grant_type, appid, secret);
            System.out.println(forObject);
            return forObject;
        } catch (Exception e) {
            System.out.println(e.getCause() + "===" + e.getMessage());
            return "获取access_token失败";
        }

    }

    /**
     * 获取token
     *
     * @param data
     * @return
     */
    //@RequestMapping("wx")
    public String setService(WxData data) {
        try {
            if (data == null || data.toString() == "") {
                return "没有获取数据";
            }
            String[] dataArray = new String[3];
            dataArray[0] = data.getTimestamp();
            dataArray[1] = data.getNonce();
            dataArray[2] = token;
            //字典排序
            Arrays.sort(dataArray);
            // 2. 将三个参数字符串拼接成一个字符串进行sha1加密
            StringBuilder content = new StringBuilder();
            for (int i = 0; i < dataArray.length; i++) {
                content.append(dataArray[i]);
            }
            System.out.println(data.getEchostr() + "===" + data.getTimestamp() + "==" + data.getNonce() + "===" + token);
            //加密
            messageDigest = MessageDigest.getInstance("SHA-1");
            messageDigest.update(content.toString().getBytes());
            String hashCode = byte2Hex(messageDigest.digest());
            System.out.println(hashCode + "=======" + data.getSignature());
            if (data.getSignature().equals(hashCode)) {
                return data.getEchostr();
            } else {
                return "数据错误";
            }
        } catch (Exception e) {
            System.out.println(e.getCause() + "===" + e.getMessage());
            return "出现异常";
        }
    }

    /**
     * 将byte转为16进制
     *
     * @param bytes
     * @return
     */
    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                //1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

}
